﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Baosight.ZNH.ZMCS.UI.Utils;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 生产过程系统设置 业务过滤
    /// </summary>
    /// Title:ZMCS24
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张志芳   [创建]
    ///
    public partial class ZMCS17 : EFViewCmes
    {
        private BussinessFilterBlock block = new BussinessFilterBlock();//构造前台下拉列表
 
        public ZMCS17()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            _view.Args.Add("fdModuleNo", this.fdModuleNo.GetDataValue().ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 主表选中记录发生改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();

                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdBusinessFilterId", _row["fdKeyId"].ToString());
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCS18", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        /// <summary>
        /// 主表查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "业务过滤-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 主表新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            if (this.runType.Value == null || this.runType.Value.ToString() == "1")
            {
                base.OpenEFWindowAlert("不能新增业务过滤记录！", true);
                return;
            }

            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "业务过滤-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 主表修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            if (this.runType.Value == null || this.runType.Value.ToString() == "1")
            {
                base.OpenEFWindowAlert("不能修改业务过滤记录！", true);
                return;
            }

            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "业务过滤-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            if (this.runType.Value == null || this.runType.Value.ToString() == "1")
            {
                base.OpenEFWindowAlert("不能删除业务过滤记录！", true);
                return;
            }

            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 主表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 主表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            BusinessFilter businessFilter = new BusinessFilter(_dialogType);//构造一个新的修改页面
            businessFilter.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(block.GetFilterType());//过滤类型
            _eIInfo.AddBlock(block.GetLevel());//过滤级别
            businessFilter.EIDataContext.Update(_eIInfo);

            businessFilter.fdModuleNo.Value = this.fdModuleNo.Value;//模块编号

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = businessFilter;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 执行删除后的页面操作
        /// 包括主表删除和从表删除，重写删除后的回调函数
        /// </summary>
        public override void DoDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            base.DoDeleteOperationCompleted(sender, e);

            if (this.Grid.SelectedIndex == -1 || this.Grid.SelectedItem == null)
            {
                this.container.Content = null;
            }
        }

    }
}
